import { login, getProfile, getProfileHead } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    // 取 Cookies token
    token: getToken() || '',
    // 登录弹框消息判断
    Message: true,
    userInfoList: {}
  },
  mutations: {
    // 修改登录弹框消息判断
    MessageLogin(state, newMessage) {
      state.Message = newMessage
    },
    // 存 Cookies token
    getTokenQ(state, newToken) {
      state.token = newToken
      setToken(newToken)
    },
    // 删 Cookies token
    removeToken(state) {
      state.token = ''
      removeToken()
      state.userInfoList = {}
    },
    // 存用户信息
    setuserInfoList(state, val) {
      state.userInfoList = val
    }

  },
  actions: {
    // 获取token
    async getTokenLogin(context, par) {
      const res = await login(par)
      context.commit('getTokenQ', res.data)
    },
    // 获取用户资料数据
    async getProfileApi(context) {
      const res = await getProfile()
      context.commit('setuserInfoList', res.data)
    },
    // 获取用户头像
    async getProfileHead(context) {
      const id = context.state.userInfoList.userId
      const res = await getProfileHead(id)
      const arr = context.state.userInfoList
      const Arr = { ...res.data, ...arr }
      context.commit('setuserInfoList', Arr)
    },
    // 删除用户数据
    logout(context) {
      context.commit('removeToken')
    }
  }
}

